import time
import BaseHTTPServer
import urllib2

HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 80 # Maybe set this to 9000.

opener = urllib2.build_opener(urllib2.ProxyHandler({'https':'//localhost:8118'}))
urllib2.install_opener(opener)
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(s):
        s.send_response(200)
        s.send_header("Content-type", "text/xml")
        s.end_headers()
        s.wfile.write(open('settings.xml').read())
    def do_GET(s):
        h = urllib2.urlopen('https://221.130.45.201%s' % s.path)
        print '\n'.join(key + ':\t\t' + h.headers[key] for key in h.headers.keys())
        """Respond to a GET request."""
        s.send_response(200)
        s.send_header("Content-type", "text/xml")
        #s.send_header("Set-Cookie", "ssic=DAgHAAAfZyk91WfLbeN3dd1QQoNREc23sZcRH1brChQHUDQE7IOZdc3kTWD9xgu+q1iMyQqbZVDtGTCLvVG1O+0UfvadX/zavoIGZXDLrE/VKJ2a/w==")
        s.send_header("Set-Cookie", h.headers['Set-Cookie'])
        s.end_headers()
        s.wfile.write('''<?xml version="1.0" encoding="utf-8" ?><results status-code="200"><user uri="sip:670546214@fetion.com.cn;p=3545" mobile-no="13764481777" user-status="101"><credentials></credentials></user></results>''')

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)